<template>
<!-- 大的遮罩层 -->
  <div class="dialog" v-show="chang" @click.self="close" >
    <!-- 头部 -->
    <div class="wrap">
      <div class="header">
        <!-- 头部-左边 -->
        <p class="title">
          <!-- 命名插槽 -->
          <slot name="headerOne" />
        </p>
        <!-- 头部-右边 -->
        <button class="btn" @click="close">X</button>
      </div>
      <!-- 中间 -->
      <div class="dialog-body">
        <!-- 默认插槽 -->
        <slot />
      </div>
      <!-- 尾部 -->
      <div class="footer">
        <!-- 命名插槽 -->
        <slot name="footerOne" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Dialog",
  // 2.传参,实现弹窗的显示隐藏效果
  props:{
    chang:{
      type:Boolean,
      required:true,
    }
  },
  // 1.给"删除",一个自定义事件
  methods:{
   close(){
     this.$emit("update:chang",false);
   }
  },
  // 3.弹窗出现时,禁用滚动条
  watch: {
    chang: {
      handler(chang) {
        if (chang) {
          // 禁用滚动条
          // document.documentElement.style.overflow="hidden"
          document.documentElement.style["overflow-y"] = "hidden";
        } else {
          // 启用滚动条
          document.documentElement.style["overflow-y"] = "auto";
        }
      },
      //只触发一次 
      immediate: true,
    },
  },
};
</script>

<style lang="less" scoped>
.dialog {
  position: fixed;
  // 将其遮罩层高度撑开
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.3);
}
.wrap {
  // 垂直水平居中
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  padding: 10px 20px;
  box-sizing: border-box;
  border-radius: 10px;
  background: pink;
}
.header {
  display: flex;
  justify-content: space-between;
}
.title {
  font-size: 20px;
}
.btn {
  border: none;
  outline: none;
  background-color: pink;
  cursor: pointer;
  &:hover {
    color: red;
  }
}
.dialog-body {
  padding: 20px 0;
  font-size: 16px;
}
</style>
